﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using 海漫思考核.BLL;
using 海漫思考核.DAL;

namespace 海漫思考核
{
    public partial class EditNove1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)//第一次加载
            {
                if (Session["User"] != null)
                {
                    //给前端类型下拉框赋值
                    var types = NoveBLL.SelectAllNoveType();
                    foreach (NoveType item in types)
                    {
                        ddl_type.Items.Add(item.Tname);
                        ddl_type.Items[item.Tid - 1].Value = item.Tid.ToString();
                    }
                    //获取对应的小说信息并赋值给前端控件
                    int id = int.Parse(Request["id"]);
                    var nove = NoveBLL.SelectNoveByID(id);
                    span_id.InnerHtml = nove.Nid.ToString();
                    text_name.Value = nove.Nname;
                    text_author.Value = nove.Nauthor;
                    text_chapter.Value = nove.Nchapter.ToString();
                    img.Src = nove.Nimgpath;
                    text_uptime.Value = Convert.ToDateTime(nove.Nuptime).ToString("yyyy-MM-dd");
                    ddl_type.SelectedValue = nove.Ntype.ToString();
                    ddl_state.SelectedValue = nove.Nstate.ToString();
                }
                else
                {
                    Response.Write("<script>alert('非法登录!即将返回登录页面!');location.href = 'Login.aspx';</script>");
                }
            }
        }
        /// <summary>
        /// 更新点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_update_Click(object sender, EventArgs e)
        {
            //创建小说对象
            NoveInfo nove = new NoveInfo()
            {
                Nid = int.Parse(span_id.InnerHtml),
                Nname = text_name.Value,
                Nauthor = text_author.Value,
                Nchapter = int.Parse(text_chapter.Value),
                Nuptime = Convert.ToDateTime(text_uptime.Value),
                Ntype = int.Parse(ddl_type.SelectedValue),
                Nstate = int.Parse(ddl_state.SelectedValue)
            };
            //执行更新命令并接受返回值
            if (NoveBLL.NoveUpdate(nove))
            {
                Response.Write("<script>alert('更新成功！');location.href = 'ManageNove.aspx'</script>");
            }
            else
            {
                Response.Write("<script>alert('更新失败')</script>");
            }
        }
        /// <summary>
        /// 返回点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_back_Click(object sender, EventArgs e)
        {
            Response.Redirect("ManageNove.aspx");
        }
    }
}